package logic

import (
	"card/app/models"
	"card/app/requests"
	"card/pkg/myError"
	"context"
	"github.com/duke-git/lancet/v2/random"
	"github.com/gin-gonic/gin"
)

type CardRoomLogic struct {
	ctx context.Context
}

func NewCardRoom(ctx context.Context) *CardRoomLogic {
	if c, ok := ctx.(*gin.Context); ok {
		ctx = c.Request.Context()
	}
	return &CardRoomLogic{ctx: ctx}
}

func (l *CardRoomLogic) CreateRoom(params *requests.RoomRequest) (string, error) {
	memberLogic := &MemberLogic{ctx: l.ctx}
	cardRoomModel := models.NewCardRoom(l.ctx)
	cardRoomModel.UserID = memberLogic.CurrentUserId()
	cardRoomModel.Num = params.Num
	cardRoomModel.Rule = params.Rule
	cardRoomModel.RoomNum = random.RandNumeral(6)
	_, err := cardRoomModel.Create()
	if err != nil {
		return "", myError.WarpError(err)
	}
	return cardRoomModel.RoomNum, nil
}
